﻿
using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Data.EntityClient;
using PhotoGalleryEnteties;

namespace EntityProvider
{
    public partial class PhotogalleryContext : ObjectContext,IContext 
    {
        public const string ConnectionString = "name=photogalleryEntities";
        public const string ContainerName = "photogalleryEntities";

        #region Constructors

        public PhotogalleryContext()
            : base(ConnectionString, ContainerName)
        {
            this.ContextOptions.LazyLoadingEnabled = true;
        }

        public PhotogalleryContext(string connectionString)
            : base(connectionString, ContainerName)
        {
            this.ContextOptions.LazyLoadingEnabled = true;
        }

        public PhotogalleryContext(EntityConnection connection)
            : base(connection, ContainerName)
        {
            this.ContextOptions.LazyLoadingEnabled = true;
        }

        #endregion

        #region ObjectSet Properties

        public ObjectSet<Album> Albums
        {
            get { return _albums ?? (_albums = CreateObjectSet<Album>("Albums")); }
        }
        private ObjectSet<Album> _albums;

        public ObjectSet<PhotoComment> PhotoComments
        {
            get { return _photoComments ?? (_photoComments = CreateObjectSet<PhotoComment>("PhotoComments")); }
        }
        private ObjectSet<PhotoComment> _photoComments;

        public ObjectSet<PhotoInfo> Photos
        {
            get { return _photos ?? (_photos = CreateObjectSet<PhotoInfo>("Photos")); }
        }
        private ObjectSet<PhotoInfo> _photos;

        public ObjectSet<Tag> Tags
        {
            get { return _tags ?? (_tags = CreateObjectSet<Tag>("Tags")); }
        }
        private ObjectSet<Tag> _tags;

        public ObjectSet<GalleryUser> GalleryUsers
        {
            get { return _galleryUsers ?? (_galleryUsers = CreateObjectSet<GalleryUser>("GalleryUsers")); }
        }
        private ObjectSet<GalleryUser> _galleryUsers;

        #endregion

        #region Function Imports

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        /// <param name="n">No Metadata Documentation available.</param>
        public IEnumerable<PhotoInfo> GetRandomPhotos(int count)
        {
            ObjectParameter nParameter = new ObjectParameter("n", count);
            return base.ExecuteFunction<PhotoInfo>("GetRandomPhotos", nParameter);
        }
 

       

      
         public void AddSubscribeForComments(Guid user, int photo, DateTime observeDate)
        {
            ObjectParameter photoIdParameter = new ObjectParameter("PhotoId", photo );
            ObjectParameter userIdParameter = new ObjectParameter("UserId", user );
            ObjectParameter observeDateParameter = new ObjectParameter("ObserveDate", observeDate);
            ExecuteFunction("AddSubscribeForComments", photoIdParameter, userIdParameter, observeDateParameter);
        }




       public IEnumerable<Guid> SubscribedUsersIds(int photo)
        {
            ObjectParameter  photoIdParameter = new ObjectParameter("PhotoId",photo  );
         

            return ExecuteFunction<Guid>("SubscribedUsersForPhoto", photoIdParameter);
        }

        public bool  UpdateSubscribe(Guid user, int photo, DateTime observeDate)
        {
            ObjectParameter photoIdParameter = new ObjectParameter("PhotoId", photo );
            ObjectParameter userIdParameter = new ObjectParameter("UserId", user );
            ObjectParameter observeDateParameter = new ObjectParameter("ObserveDate", observeDate);
            ObjectParameter resultParameter = new ObjectParameter("Result", typeof (int));
            ExecuteFunction("UpdateSubscribe", photoIdParameter, userIdParameter, observeDateParameter,resultParameter );
            return (int)resultParameter.Value > 0;

        }

        public bool SubscribeExists(int photo, Guid user)
        {
            ObjectParameter photoIdParameter = new ObjectParameter("PhotoId", photo);
            ObjectParameter userIdParameter = new ObjectParameter("UserId", user);
            return ExecuteFunction("SubscribeExists", photoIdParameter, userIdParameter) == 0 ? false : true;
        }


      
        public IEnumerable<PhotoInfo > RecentCommented(Guid userId)
        {
            ObjectParameter userIdParameter = new ObjectParameter("UserId", userId);
            return base.ExecuteFunction<PhotoInfo>("RecentCommented", userIdParameter);

        }

        public IEnumerable< string > GetAllowedRoles()
        {
            return base.ExecuteFunction<string>("AllowedRoles");
        }

        #endregion

    }
}

